//
//  MoveAnamition.m
//  MyMoveAnimation
//
//  Created by 李佳 on 15/5/5.
//  Copyright (c) 2015年 LiJia. All rights reserved.
//

#import "MoveAnamition.h"

@implementation MoveAnamition
- (instancetype)initWithFrame:(CGRect)frame andMovePath: (CGPathRef)path
{
    if (self =[super initWithFrame:frame])
    {
        [self initAnimationEmitter: path];
        //[self initAnimationMoveWithPath:path];
        
    }
    return self;
}

- (void)initAnimationEmitter: (CGPathRef)path
{
    CAEmitterLayer *emitterLayer = [[CAEmitterLayer alloc] init];
    
    emitterLayer.emitterPosition = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2);    // 坐标
    emitterLayer.emitterSize = self.bounds.size;            // 粒子大小
    emitterLayer.renderMode = kCAEmitterLayerBackToFront;      // 递增渲染模式
    emitterLayer.emitterMode = kCAEmitterLayerPoints;       // 粒子发射模式（面发射）
    emitterLayer.emitterShape = kCAEmitterLayerSphere;      // 粒子形状（球状）
    
    // 星星粒子
    CAEmitterCell *cell1 = [self productEmitterCellWithContents:(id)[[UIImage imageNamed:@"star1.png"] CGImage]];
    cell1.scale = 0.3;
    cell1.scaleRange = 0.1;
    
    // 圆粒子
    CAEmitterCell *cell2 = [self productEmitterCellWithContents:(id)[[UIImage imageNamed:@"cycle1.png"] CGImage]];
    cell2.scale = 0.05;
    cell2.scaleRange = 0.02;
    
    emitterLayer.emitterCells = @[cell1, cell2];
    
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"emitterPosition"];
    animation.path = path;
    animation.duration = 1;
    animation.repeatCount = 1;
    [emitterLayer addAnimation:animation forKey:nil];
    [self.layer addSublayer:emitterLayer];
}

- (CAEmitterCell *)productEmitterCellWithContents:(id)contents
{
    CAEmitterCell *cell = [CAEmitterCell emitterCell];
    cell.birthRate = 50;       // 每秒产生粒子数
    cell.lifetime = 0.2;          // 每个粒子的生命周期
    cell.lifetimeRange = 0.2;
    cell.contents = contents;   // cell内容，一般是一个CGImage
    cell.color = [[UIColor whiteColor] CGColor];
    cell.velocity = 50;         // 粒子的发射方向
    cell.emissionLongitude = M_PI*2;
    cell.emissionRange = M_PI*2;
    cell.velocityRange = 10;
    cell.spin = 10;
    
    return cell;
}

- (void)initAnimationMoveWithPath: (CGPathRef)path
{

}

@end
